import numpy as np
from PPMCC import PPMCC
def find_rating_byMovieAndUser(ford_train_data,ford_user_index,userId,movieId):
    user_start=ford_user_index[userId]
    user_end=ford_user_index[userId+1]
    for i in range(user_start,user_end):
        if ford_train_data['movieId'][i]==movieId:
            return ford_train_data['rating'][i]
def find_users_liked_currentmovie(ford_train_data,ford_user_index,movieId,rating_threshold):
    m=ford_user_index.shape[0]-2 #总用户数
    # 以4、5星评价作为该用户的喜好偏向
    current_movie_liked_by_users=[]
    # 发现喜欢当前movieID的所有用户
    for i in range(m):
        user_start = ford_user_index[i + 1]
        user_end = ford_user_index[i + 2]  # end不包含在内
        user_rated_movies = ford_train_data['movieId'][user_start:user_end]
        movie_rating = ford_train_data['rating'][user_start:user_end]
        movieId_index = np.where(user_rated_movies == movieId)
        if movieId_index[0].shape[0]!=0:
            if movie_rating[movieId_index[0][0]] >= rating_threshold:
                current_movie_liked_by_users.append(i + 1)
    current_movie_liked_by_users = np.array(current_movie_liked_by_users)
    return current_movie_liked_by_users
def find_movies_liked_by_user(ford_train_data,ford_user_index,userId,rating_threshold):
    current_user_start=ford_user_index[userId]
    current_user_end=ford_user_index[userId+1] #end不包含在内
    like_list=[]
    for i in range(current_user_start,current_user_end):
        if ford_train_data['rating'][i]>=rating_threshold:
            like_list.append(ford_train_data['movieId'][i])
    like_list=np.array(like_list)
    return like_list

def find_g_similar_movies(g,beta,ford_train_data,ford_user_index,userId,movieId):
    rating_threshold=4
    current_user_start=ford_user_index[userId]
    current_user_end=ford_user_index[userId+1] #end不包含在内
    while 1:
        current_movielist=find_movies_liked_by_user(ford_train_data,ford_user_index,userId,rating_threshold)
        current_userslist=find_users_liked_currentmovie(ford_train_data,ford_user_index,movieId,rating_threshold)
        if current_movielist.shape[0]>=g:
            if current_movielist.shape[0]>g:
                similarity=[]
                for i in range(current_movielist.shape[0]):
                    userlist=find_users_liked_currentmovie(ford_train_data,ford_user_index,current_movielist[i],rating_threshold)
                    results,nij=PPMCC(current_userslist,userlist)
                    similarity.append(nij*results/(nij+beta))
                similarity=np.array(similarity)
                most_similar_movies=np.argsort(similarity)[::-1]
                most_similar_movies=most_similar_movies[:g] #存储着最相关的20个电影的Id
            else:
                most_similar_movies=current_movielist
            return (most_similar_movies,current_userslist,rating_threshold,0)
        else:
            if current_movielist.shape[0]>0:
                most_similar_movies=current_movielist
                return (most_similar_movies,current_userslist,rating_threshold,0)
            else: # 如果当前user的喜好为0
                if rating_threshold>3:
                    rating_threshold-=0.5
                else:
                    return ([],current_userslist,rating_threshold,-1)